<?php
include "conf.inc";

$next = isset($_POST["next"]) ? intval($_POST["next"]) : 1;

$group_handler = xoops_getmodulehandler("group");
$cat_handler = xoops_getmodulehandler("category");
$perm_handler = xoops_getmodulehandler("permission");

$xoopsOption["xoops_pagetitle"] = "Create Group";
$xoopsOption["template_main"] = "groups_group_create.html";
include "header.php";

switch ($next) {
	case 4:
		$params = array();
		$params["group_id"] = isset($_POST["group_id"]) ? intval($_POST["group_id"]) : 0;
		if ( empty($params["group_id"]) ) {
			xoGroups::redirect("create.php",3,"没有选择分类, 重来");
		}
		$params["perm"] = $_POST;
		if ( $group_id = $perm_handler->updateGroupPerm($params) ) {
			
		}
		break;
	case 3:
		$params = $_POST;
		$params["uid"] = $xoopsUser->uid();
		$params["group_creator"] = $xoopsUser->uname();
		$cat_id = isset($_POST["cat_id"]) ? intval($_POST["cat_id"]) : 0;
		
		if ( empty($cat_id) ) {
			xoGroups::redirect("create.php",3,"没有选择分类, 重来");
		}
		
		// check create group fields 
		$msg = xoGroups::checkSubmitFields("group", $params, $token=true); 
		if ( true !== $msg ) {
			xoGroups::redirect("create.php", 5, $msg);
		}
		
		if ( $group_id = $group_handler->setGroup($params) ) {
			// create group default permission
			$perm_handler->setGroupDefPerm($group_id);
			// update category group total
			$cat_handler->updateGroupTotal($cat_id);
			
			// update group member
            $group_handler->updateMemberTotal(array("group_id"=>$group_id,"value"=>1));
            
            $memb_handler = xoops_getmodulehandler("memb");
            if ( $member_id = $memb_handler->setMember(array("group_id"=>$group_id,"uid"=>$xoopsUser->uid())) ) {
                $memb_handler->updateToAdmin($member_id, true);
            }
            
            // update group footprint
            $footprint_handler = xoops_getmodulehandler("footprint");
            $footprint_handler->setFootprint(array("group_id"=>$group_id,"uid"=>$xoopsUser->uid()));
            $params["footprint_display"] = 1;
            $footprint_handler->updateFields($params);
		}
		
		if ( empty($group_id) ) {
			xoGroups::redirect("create.php",3,"失败！重来");
		}
		$formUrl = "create.php";
		$group_arg = $perm_handler->getGroupPerm($group_id);
		$group_obj = $group_handler->getGroup($group_id);
		include (dirname(__FILE__)."/include/form.us.group.perm.php");
		$xoopsTpl->assign(array("group"=>$group_obj->getValues()));
		break;
	case 2:
		$cat_id = isset($_POST["cat_id"]) ? intval($_POST["cat_id"]) : 0 ;
		if ( empty($cat_id) ) {
			xoGroups::redirect("create.php",3,"没有选择分类, 重来");
		}
		$cat_obj = $cat_handler->get($cat_id);
		$group_obj = $group_handler->get();
		include (dirname(__FILE__)."/include/form.us.group.php");
		$xoopsTpl->assign(array("category"=>$cat_obj->getValues()));
		break;
	case 1;
	default:
		$categories = $cat_handler->getCategories(array("order"=>"DESC","sort"=>"cat_order"),false);
		$xoopsTpl->assign(array("categories"=>$categories));
}

$xoopsTpl->assign(array("next"=>$next));
include "footer.php";